<script setup>
import AudioRecorder from './components/AudioRecorder.vue'
import backgroundImage from './assets/background.jpg'
import { ref } from 'vue'

const recognizedText = ref('')

// 修改为数组存储对话历史
const chatHistory = ref([])

// 更新对话历史的方法
const updateChat = (role, message) => {
  chatHistory.value.push({
    role: role,
    content: message.content
  })
}
</script>

<template>
  <div class="app-container" :style="{ backgroundImage: `linear-gradient(rgba(255, 255, 255, 0.5), rgba(255, 255, 255, 0.5)), url(${backgroundImage})` }">
    <div class="chat-container">
      <div class="messages-area">
        <!-- 展示完整对话历史 -->
        <template v-for="(message, index) in chatHistory" :key="index">
          <div :class="['message', message.role === 'ai' ? 'ai-message' : 'user-message']">
            <span>{{ message.role === 'ai' ? 'AI' : '我' }}: {{ message.content }}</span>
          </div>
        </template>
      </div>
      
      <div class="control-area">
        <h1>AI语音助手</h1>
        <AudioRecorder 
          @update:recognizedText="message => updateChat('human', message)" 
          @update:aiResponse="message => updateChat('ai', message)" 
        />
      </div>
    </div>
  </div>
</template>

<style>
/* 全局样式 */
html, body {
  margin: 0;
  padding: 0;
  overflow: hidden;
  height: 100vh;
  width: 100vw;
  position: fixed;
  touch-action: none;  /* 禁止触摸滑动 */
}
</style>

<style scoped>
.app-container {
  height: 100vh;
  width: 100vw;
  overflow: hidden;
  position: fixed;
  top: 0;
  left: 0;
  background-size: cover;
  background-position: center;
  background-attachment: fixed;
  display: flex;
  flex-direction: column;
  touch-action: none;  /* 禁止触摸滑动 */
}

.chat-container {
  height: 100%;
  display: flex;
  flex-direction: column;
  padding-bottom: 140px;  /* 调整底部间距 */
  overflow: hidden;
}

.messages-area {
  flex: 1;
  max-height: calc(100vh - 250px);
  overflow-y: auto;
  padding: 2rem;
  display: flex;
  flex-direction: column;
  gap: 1rem;
  margin: 1rem;
  background: rgba(255, 255, 255, 0.6);
  backdrop-filter: blur(6px);
  border-radius: 16px;  /* 调整消息区域的圆角 */
}

.message {
  max-width: 80%;
  padding: 1rem;
  border-radius: 1rem;
  box-shadow: 0 2px 4px rgba(0, 0, 0, 0.1);
  word-break: break-word;
}

.user-message {
  align-self: flex-end;
  background-color: #e3f2fd;
  margin-left: 20%;
}

.ai-message {
  align-self: flex-start;
  background-color: #f5f5f5;
  margin-right: 20%;
}

.control-area {
  position: fixed;
  bottom: 10px;
  left: 10px;
  right: 10px;
  height: 120px;
  background: rgba(255, 255, 255, 0.6);
  backdrop-filter: blur(6px);
  padding: 0.5rem 1rem;  /* 减小上下内边距 */
  box-shadow: 0 -2px 10px rgba(0, 0, 0, 0.1);
  z-index: 100;
  border-radius: 16px;
  margin: 0 auto;
  max-width: calc(100% - 20px);
  display: flex;          /* 添加弹性布局 */
  flex-direction: column; /* 垂直排列 */
  justify-content: flex-start; /* 内容靠上对齐 */
}

h1 {
  color: #ff69b4;
  margin: 0.2rem 0;    /* 调整标题上下间距 */
  font-size: 1.5rem;
  text-shadow: 2px 2px 4px rgba(0, 0, 0, 0.1);
}
</style>
